<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编码" prop="devCode">
        <el-input
          v-model="queryParams.devCode"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="devName">
        <el-input
          v-model="queryParams.devName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option v-for="item in firHydrantOptions" :key="item.dictValue" :label="item.dictValue" :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="园区" prop="parkId">
        <el-select clearable v-model="queryParams.parkId" @change="getBuildingList(form.parkId)" placeholder="请选择园区">
          <el-option
            v-for="item in buiParkList"
            :key="item.id"
            :label="item.parkName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋" prop="buildingId">
        <el-select
          clearable
          v-model="queryParams.buildingId"
          placeholder="请选择楼栋"
        >
          <el-option
            v-for="item in buildingList"
            :key="item.id"
            :label="item.buildingName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投用日期">
        <el-date-picker
          v-model="daterangeUseDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="devStatus">
        <el-select v-model="queryParams.devStatus" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in devStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['security:afFirHydrant:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['security:afFirHydrant:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['security:afFirHydrant:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['security:afFirHydrant:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afFirHydrantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编码" align="center" prop="devCode"/>
      <el-table-column label="名称" align="center" prop="devName"/>
      <el-table-column label="规格" align="center" prop="specification"/>
      <el-table-column label="类型" align="center" prop="type"/>
      <el-table-column label="生产厂家" align="center" prop="factory"/>
      <el-table-column label="园区" align="center" prop="parkName"/>
      <el-table-column label="楼栋" align="center" prop="buildingName"/>
      <el-table-column label="位置" align="center" prop="location"/>
      <el-table-column label="投用日期" align="center" prop="useDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="devStatus" :formatter="devStatusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['security:afFirHydrant:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['security:afFirHydrant:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改消防栓台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资产类型" v-if="!form.id" prop="sysBmId">
          <!--          <BmGenSelect @select="genCode"></BmGenSelect>-->
          <treeselect v-model="form.sysBmId" :options="sysBmTypeOptions" :normalizer="bmNormalizer"
                      @select="genCode" placeholder="选择资产类型"/>
        </el-form-item>
        <el-form-item label="名称" prop="devName">
          <el-input v-model="form.devName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option v-for="item in firHydrantOptions" :key="item.dictValue" :label="item.dictValue" :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产厂家" prop="factory">
          <el-input v-model="form.factory" placeholder="请输入生产厂家"/>
        </el-form-item>
        <el-form-item label="园区" prop="parkId">
          <el-select clearable v-model="form.parkId" @change="getBuildingList(form.parkId)" placeholder="请选择园区">
            <el-option
              v-for="item in buiParkList"
              :key="item.id"
              :label="item.parkNum"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋" prop="buildingId">
          <el-select
            clearable
            v-model="form.buildingId"
            placeholder="请选择楼栋"
          >
            <el-option
              v-for="item in buildingList"
              :key="item.id"
              :label="item.buildingName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置"/>
        </el-form-item>
        <el-form-item label="投用日期" prop="useDate">
          <el-date-picker clearable size="small"
                          v-model="form.useDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择投用日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.devStatus">
            <el-radio
              v-for="dict in devStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
        <el-divider content-position="center">消防栓配套信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddAfHydrantDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteAfHydrantDetail">删除
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="afHydrantDetailList" :row-class-name="rowAfHydrantDetailIndex"
                  @selection-change="handleAfHydrantDetailSelectionChange" ref="afHydrantDetail">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="配套名称" prop="detailName">
            <template slot-scope="scope">
              <el-input v-model="scope.row.detailName" placeholder="请输入配套名称"/>
            </template>
          </el-table-column>
          <el-table-column label="配套规格" prop="detailSpecification">
            <template slot-scope="scope">
              <el-input v-model="scope.row.detailSpecification" placeholder="请输入配套规格"/>
            </template>
          </el-table-column>
          <el-table-column label="配套数量" prop="detailNum">
            <template slot-scope="scope">
              <el-input v-model="scope.row.detailNum" placeholder="请输入配套数量"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAfFirHydrant,
  getAfFirHydrant,
  delAfFirHydrant,
  addAfFirHydrant,
  updateAfFirHydrant
} from '@/api/smartParks/security/securityFirHydrant'
import { listBuiPark } from '@/api/parkAssets/enterCommunity'
import { listBuiBuilding } from '@/api/parkAssets/building'

export default {
  name: 'AfFirHydrant',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedAfHydrantDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消防栓台账表格数据
      afFirHydrantList: [],
      // 消防栓配套表格数据
      afHydrantDetailList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 投用日期时间范围
      daterangeUseDate: [],
      // 状态字典
      devStatusOptions: [],
      //消防栓类型
      firHydrantOptions:[],

      sysBmTypeOptions: this.$store.state.permission.sysBmTree,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        devCode: null,
        devName: null,
        type: null,
        parkId: null,
        buildingId: null,
        location: null,
        useDate: null,
        devStatus: null
      },
      buiParkList: [],

      buildingList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getDicts('device_work_state').then(response => {
      this.devStatusOptions = response.data
    });
    this.getDicts("firhydrant_type").then(response => {
      this.firHydrantOptions = response.data
    })

    listBuiPark().then((response) => {
      this.buiParkList = response.rows
    })

  },
  methods: {

    genCode(item) {
      console.log('item', item)
      this.form.sysBmId = item.bmId
    },

    //得到楼栋
    getBuildingList(value) {
      this.form.buildingId = ''
      this.buildingList = []
      listBuiBuilding({ parkId: value }).then((response) => {
        this.buildingList = response.rows
      })
    },


    /** 查询消防栓台账列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeUseDate && '' != this.daterangeUseDate) {
        this.queryParams.params['beginUseDate'] = this.daterangeUseDate[0]
        this.queryParams.params['endUseDate'] = this.daterangeUseDate[1]
      }
      listAfFirHydrant(this.queryParams).then(response => {
        this.afFirHydrantList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 状态字典翻译
    devStatusFormat(row, column) {
      return this.selectDictLabel(this.devStatusOptions, row.devStatus)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sysBmId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        devCode: null,
        devName: null,
        specification: null,
        type: null,
        factory: null,
        parkId: null,
        buildingId: null,
        location: null,
        useDate: null,
        devStatus: '0',
        remark: null,
      }
      this.afHydrantDetailList = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeUseDate = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加消防栓台账'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAfFirHydrant(id).then(response => {
        this.form = response.data
        this.afHydrantDetailList = response.data.afHydrantDetailList
        this.open = true
        this.title = '修改消防栓台账'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.afHydrantDetailList = this.afHydrantDetailList
          if (this.form.id != null) {
            updateAfFirHydrant(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAfFirHydrant(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除消防栓台账编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delAfFirHydrant(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 消防栓配套序号 */
    rowAfHydrantDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 消防栓配套添加按钮操作 */
    handleAddAfHydrantDetail() {
      let obj = {}
      obj.detailName = ''
      obj.detailSpecification = ''
      obj.detailNum = ''
      this.afHydrantDetailList.push(obj)
    },
    /** 消防栓配套删除按钮操作 */
    handleDeleteAfHydrantDetail() {
      if (this.checkedAfHydrantDetail.length == 0) {
        this.$alert('请先选择要删除的消防栓配套数据', '提示', { confirmButtonText: '确定' })
      } else {
        this.afHydrantDetailList.splice(this.checkedAfHydrantDetail[0].index - 1, 1)
      }
    },
    /** 单选框选中数据 */
    handleAfHydrantDetailSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.afHydrantDetail.clearSelection()
        this.$refs.afHydrantDetail.toggleRowSelection(selection.pop())
      } else {
        this.checkedAfHydrantDetail = selection
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('security/afFirHydrant/export', {
        ...this.queryParams
      }, `security_afFirHydrant.xlsx`)
    }
  }
}
</script>
